package logic

import (
	"context"
	"fmt"
	"shuqi-novel/model/mysql"
	"shuqi-novel/novelrpc/internal/svc"
	"shuqi-novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookAddLogic {
	return &BookAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookAddLogic) BookAdd(in *novelrpc.BookAddRequest) (*novelrpc.BookAddResponse, error) {
	// todo: add your logic here and delete this line

	var book mysql.Book
	mysql.DB.Debug().Where("title=?", in.Title).Find(&book)

	if book.ID != 0 {
		return nil, fmt.Errorf("已添加过了,请重新添加")
	}

	data := mysql.Book{
		UserId: int(in.UserId),
		Title:  in.Title,
		Text:   in.Text,
		Type:   in.Type,
	}
	mysql.DB.Create(&data)
	return &novelrpc.BookAddResponse{
		BookId: int64(data.ID),
	}, nil
}
